<?php

/**
 * This is the model class for table "hinh".
 *
 * The followings are the available columns in table 'hinh':
 * @property integer $id
 * @property integer $id_album
 * @property string $ten_hinh
 * @property string $mo_ta
 * @property string $url
 * @property string $thoi_diem_upload
 *
 * The followings are the available model relations:
 * @property BinhLuanHinh[] $binhLuanHinhs
 * @property Album $idAlbum
 * @property HinhLike[] $hinhLikes
 * @property HinhTag[] $hinhTags
 */
class Hinh extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Hinh the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'hinh';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_album, ten_hinh, mo_ta, url, thoi_diem_upload', 'required'),
			array('id_album', 'numerical', 'integerOnly'=>true),
			array('ten_hinh, url', 'length', 'max'=>100),
			array('mo_ta', 'length', 'max'=>500),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, id_album, ten_hinh, mo_ta, url, thoi_diem_upload', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'binhLuanHinhs' => array(self::HAS_MANY, 'BinhLuanHinh', 'id_hinh'),
			'idAlbum' => array(self::BELONGS_TO, 'Album', 'id_album'),
			'hinhLikes' => array(self::HAS_MANY, 'HinhLike', 'id_hinh'),
			'hinhTags' => array(self::HAS_MANY, 'HinhTag', 'id_hinh'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_album' => 'Id Album',
			'ten_hinh' => 'Ten Hinh',
			'mo_ta' => 'Mo Ta',
			'url' => 'Url',
			'thoi_diem_upload' => 'Thoi Diem Upload',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('id_album',$this->id_album);
		$criteria->compare('ten_hinh',$this->ten_hinh,true);
		$criteria->compare('mo_ta',$this->mo_ta,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('thoi_diem_upload',$this->thoi_diem_upload,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                    'pagination' => array(
                        'pageSize' => 1000,
                        ),
		));
	}
        
        public function searchByOrder()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
                
                $criteria->order = 'id desc';
                $criteria->limit = 12;
                
		$criteria->compare('id',$this->id);
		$criteria->compare('id_album',$this->id_album);
		$criteria->compare('ten_hinh',$this->ten_hinh,true);
		$criteria->compare('mo_ta',$this->mo_ta,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('thoi_diem_upload',$this->thoi_diem_upload,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                    'pagination' => array('pageSize' => 12),
		));
	}
}